package com.njlime.member.controller.sys;

import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.PageBean;
import com.njlime.core.sdk.wechat.user.UserLable;
import com.njlime.member.entiies.TMemberGroup;
import com.njlime.member.service.sys.MemberGroupService;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Controller
@RequestMapping(value = "memberGroup")
public class MemberGroupController extends BaseController {

    @Resource
    MemberGroupService memberGroupService;
    /**
     * 分页列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String list(     ) {
        TMemberGroup info = new TMemberGroup();
     int unitId = UserUtils.getUser().getRootUnit().getId();

        info.setUnit_id(unitId);
        return toJsonListData(memberGroupService.list( info ));
    }

    /**
     * 分组新增
     * @param request
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "add")
    public String add(HttpServletRequest request,TMemberGroup info ){
        try {
           int unitId = UserUtils.getUser().getRootUnit().getId();

            int id = UserLable.createLable(unitId,info.getName());
            if(id == -1){
                return toJsonErrorData(ExceptionState.PARAM_EXCEPTION,"创建分组失败");
            }
            info.setId(null);
            info.setUnit_id(unitId);
            info.setCount(0);
            info.setDel(0);
            info.setGroup_id(id);
            info.setCreate_time(new Date().getTime()/1000);
            info.setUpdate_time(new Date().getTime()/1000);
            memberGroupService.add(info);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 分组修改
     * @param request
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit")
    public String edit(HttpServletRequest request,TMemberGroup info ){
        try {
            int unitId = UserUtils.getUser().getRootUnit().getId();

            TMemberGroup group =     memberGroupService.findOne(info.getId());
            int id = UserLable.editLable(unitId,info.getName(),group.getGroup_id());
            if(id == -1){
                return toJsonErrorData(ExceptionState.PARAM_EXCEPTION,"修改分组失败");
            }
            info.setUnit_id(unitId);
            memberGroupService.edit(info);
            return toJsonSuccessData();
        } catch (Exception e) {
            logger.error("系统出错", e);
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, "系统错误");
        }
    }
    /**
     * 总分组
     * @param pageBean
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getSumMember", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String getSumMember(PageBean pageBean, TMemberGroup info   ) {
         int unitId = UserUtils.getUser().getRootUnit().getId();

        info.setUnit_id(unitId);
        return toJsonListData(memberGroupService.getSumMember( info ));
    }
    /**
     * 删除分组
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "deleteGroup", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteGroup(String id, String groupId, HttpServletRequest request, HttpServletResponse response) {
        try {
            int unitId = UserUtils.getUser().getRootUnit().getId();
            int num = UserLable.deleteLable(unitId,groupId);
            if(num == -1){
                return toJsonErrorData(ExceptionState.PARAM_EXCEPTION,"删除分组失败");
            }
            memberGroupService.deleteGroup(id,unitId);
            return toJsonSuccessData();
        } catch (Exception e) {
            logger.error("", e);
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }
}
